﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace app.Api
{
    /*
        公用接口
    */
    public class ApiCommon : ApiBase
    {
        public ApiCommon(Controller ctx) : base(ctx) { }

        public void IndexMessage(string action)
        {
            switch (action)
            {
                case "getMessage":
                    {
                        ResponseJson(new ObjectResult { data = G.CacheDic["index"] }, "id,title,create_time,username,logo,coordinate,enterprise_name,enterprise_profile,createtime,p_type");
                        break;
                    }
                default: break;
            }
        }

        // 定时器要调用的函数
        public static void IndexTimer(object state)
        {
            int news_page_size = 8;
            int serviceMember_page_size = 5;
            int company_page_size = 5;
            int service_page_size = 8;
            int plateformservice_page_size = 8;
            int demand_page_size = 8;
            int abutment_page_size = 8;

            // 通知公告,新闻资讯,政策法规
            SqlResult<Article> notice = G.DB.Get<Article>("1=1 and p_type = 'notice' and status = 1", "id desc", news_page_size);
            List<Article> noticeList = notice.GetFirstTableList();
            SqlResult<Article> news = G.DB.Get<Article>("1=1 and p_type = 'news' and status = 1", "id desc", news_page_size);
            List<Article> newsList = news.GetFirstTableList();
            SqlResult<Article> laws = G.DB.Get<Article>("1=1 and p_type = 'laws'and status = 1", "id desc", news_page_size);
            List<Article> lawsList = laws.GetFirstTableList();


            Dictionary<string, List<Article>> dictionary = new Dictionary<string, List<Article>>
            {
                { "通知公告", noticeList },
                { "新闻资讯", newsList },
                { "政策法规", lawsList }
            };

            //新闻总排行
            SqlResult<Article> lastest = G.DB.Get<Article>("status = 1 ", "click_num desc", news_page_size);
            List<Article> lastestList = lastest.GetFirstTableList();

            //最新消息
            SqlResult<Article> latestNews = G.DB.Get<Article>("status = 1", "create_time desc", news_page_size);
            List<Article> latestNewsList = latestNews.GetFirstTableList();

            //服务机构
            SqlResult<Member> serviceMember = G.DB.Get<Member>("role_type = 5 and status = 2", "id desc", serviceMember_page_size);
            List<Member> serviceMemberList = serviceMember.GetFirstTableList();

            //企业
            SqlResult<Member> company = G.DB.Get<Member>("role_type = 4  and status = 2", "id desc", company_page_size);
            List<Member> companyList = company.GetFirstTableList();

            //服务
            SqlResult<ServiceItem> service = G.DB.Get<ServiceItem>("status = 2", "id desc", service_page_size);
            List<ServiceItem> serviceList = service.GetFirstTableList();

            //平台服务
            SqlResult<PlateformService> plateformservice = G.DB.Get<PlateformService>("status = 2", "id desc", plateformservice_page_size);
            List<PlateformService> plateformserviceList = plateformservice.GetFirstTableList();

            //需求
            SqlResult<DemandItem> demand = G.DB.Get<DemandItem>("status = 2", "id desc", demand_page_size);
            List<DemandItem> demandList = demand.GetFirstTableList();
            Dictionary<string, object> item = new Dictionary<string, object> {
                {"服务信息", serviceList},
                {"需求信息", demandList },
                {"平台服务", plateformserviceList },
            };

            //对接成功
            SqlResult<Abutment> success = G.DB.Get<Abutment>("demand_status = 3 and service_status = 3", "id desc", abutment_page_size);
            List<Abutment> successList = success.GetFirstTableList();

            //约谈中
            SqlResult<Abutment> introduce = G.DB.Get<Abutment>("(demand_status = 1 and (service_status = 1 or service_status = 2)) or (service_status = 1 and (demand_status = 1 or demand_status = 2))", "id desc", abutment_page_size);
            List<Abutment> introduceList = introduce.GetFirstTableList();

            //合作中
            SqlResult<Abutment> cooperation = G.DB.Get<Abutment>("(demand_status = 2 and (service_status = 2 or service_status = 3))or (service_status = 2 and (demand_status = 2 or demand_status = 3))", "id desc", abutment_page_size);
            List<Abutment> cooperationList = cooperation.GetFirstTableList();

            //所有服务机构，用于在地图上显示
            List<Member> allService = G.DB.Get<Member>("role_type=5  and status = 2").GetFirstTableList();

            Dictionary<string, object> dictionaryAbutment = new Dictionary<string, object> {
                {"对接成功", successList},
                {"约谈中", introduceList },
                {"合作中", cooperationList }
            };

            Dictionary<string, object> root = new Dictionary<string, object>();
            root.Add("navInfo", dictionary);
            root.Add("lastArticle", lastestList);
            root.Add("latestnews",latestNewsList );
            root.Add("serviceCompany", serviceMemberList);
            root.Add("Company", companyList);
            root.Add("Abutment", dictionaryAbutment);
            root.Add("serviceanddemand", item);
            root.Add("allService", allService);
            root.Add("refresh_time", DateTime.Now.ToLongTimeString());

            // 最后存到缓存中
            G.CacheDic["index"] = root;
        }

    }
}